package com.cskaoyan.mall.dao.admin;

import com.cskaoyan.mall.model.Admin;
import com.cskaoyan.mall.model.bo.admin.AdminAddBO;
import com.cskaoyan.mall.model.bo.admin.AdminLoginBO;
import com.cskaoyan.mall.model.bo.admin.ChangePwdBO;
import com.cskaoyan.mall.model.bo.admin.SearchAdminBO;
import com.cskaoyan.mall.model.vo.admin.ChangePwdVO;
import com.cskaoyan.mall.utils.DruidUtils;
import com.cskaoyan.mall.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao{
    @Override
    public int login(AdminLoginBO loginBO) {
        //JDBC
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Long query = null;
        try {
            query = (Long) runner.query("select count(id) from admin where email = ? and pwd = ?",
                    new ScalarHandler(),loginBO.getEmail(),loginBO.getPwd());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 500;
        }
        return query != 0 ? 200 : 404;
    }

    @Override
    public List<Admin> allAdmins() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query("select * from admin",new BeanListHandler<>(Admin.class));

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return admins;
    }

    @Override
    public List<Admin> searchAdmins(SearchAdminBO searchAdminBO) {
        String basesql = "select * from admin where 1=1 ";
        List<Object> list = new ArrayList<>();
        if (!StringUtils.isEmpty(searchAdminBO.getEmail())) {
            basesql = basesql + " and email like ? ";
            list.add("%" + searchAdminBO.getEmail() + "%");
        }
        if (!StringUtils.isEmpty(searchAdminBO.getNickname())) {
            basesql = basesql + " and nickname like ? ";
            list.add("%" + searchAdminBO.getNickname() + "%");
        }
        System.out.println(basesql);
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Admin> admins = null;
        try {
            admins = runner.query(basesql,new BeanListHandler<>(Admin.class),list.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return admins;
    }

    /**
     * 添加管理员账号
     * 1、首先获取数据，已经传入为adminBo
     * 2、从数据库中获取所有已经存在的数据
     * 3、判断添加的管理员邮箱是否在数据库中，如果存在，返回500
     * 4、如果不存在，就直接添加
     * @param adminAddBO
     * @return
     */
    @Override
    public int addAdminss(AdminAddBO adminAddBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into admin (email,nickname,pwd) values (?,?,?)",
                    adminAddBO.getEmail(),
                    adminAddBO.getNickname(),
                    adminAddBO.getPwd());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }

    /**
     * 修改管理员的信息
     * 1、与添加的逻辑类似
     * @param admin
     * @return
     */
    @Override
    public int updateAdminss(Admin admin) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update admin set email = ? , nickname = ? , pwd = ? where id = ? ",
                    admin.getEmail(),
                    admin.getNickname(),
                    admin.getPwd(),
                    admin.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }

    /**
     * 获取指定id的个人信息
     * @param id
     * @return
     */
    @Override
    public Admin getAdminInfo(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Admin admin = null;
        try {
            admin = runner.query("select * from admin where id = ? ", new BeanHandler<>(Admin.class), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return admin;
    }

    /**
     * 删除指定id的管理员
     * @param id
     * @return
     */
    @Override
    public int deleteAdmins(String id) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("delete from admin where id = ? ",id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }

    @Override
    public int changePwd(ChangePwdBO changePwdBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        ChangePwdVO changePwdVO = null;
        try {
            changePwdVO = runner.query("select pwd from admin where nickname = ? ",new BeanHandler<>(ChangePwdVO.class),changePwdBO.getConfirmPwd());
            if (changePwdVO.getPwd().equals(changePwdBO.getOldPwd())) {
                runner.update("update admin set pwd = ? where nickname = ? ",changePwdBO.getConfirmPwd(),changePwdBO.getAdminToken());
            }else {
                return 404;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }
}
